package com.yuan.shiro;

import com.yuan.entity.Role;
import com.yuan.entity.User;
import com.yuan.service.PermissionService;
import com.yuan.service.RoleService;
import com.yuan.service.UserService;
import com.yuan.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLOutput;
import java.util.*;

/**
 * 处理用户是否合法
 *
 * @Author yuan
 * @Date 2021/5/31 16:33
 */
@Slf4j
@Service
public class MyRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     * 用户认证
     *
     * @param auth
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException {
        String token = (String) auth.getCredentials();
        //JWTToken jwtToken= (JWTToken) auth;
        //String token=jwtToken.getToken();
        //String token=null;
        String account = JWTUtil.getUsername(token);

        if (account == null) {
            throw new AuthenticationException("token invalid");
        }
        User user = userService.findByAccount(account);
        if (user == null) {
            throw new AuthenticationException("User didn't existed");
        }
        if (!JWTUtil.verify(token, account, user.getPassword())) {
            throw new AuthenticationException("username or password error");
        }
        return new SimpleAuthenticationInfo(token, token, getName());
    }

    /**
     * 用户授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String username = JWTUtil.getUsername(principalCollection.toString());
        System.out.println(username);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        User user=userService.findByAccount(username);
        List<Role> roleList=roleService.findRoleByUserId(user.getId());
        Set<String> roleSet=new HashSet<>();
        List<Integer> roleIds=new ArrayList<>();
        roleList.forEach(role -> {
            roleSet.add(role.getRole());
            roleIds.add(role.getId());
        });
        // 放入角色信息
        authorizationInfo.setRoles(roleSet);
        // 放入权限信息
        List<String> permissionList=permissionService.findByRoleId(roleIds);
        authorizationInfo.setStringPermissions(new HashSet<>(permissionList));
        return authorizationInfo;
    }
}
